package com;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class MusicFrame extends JFrame implements ActionListener {
    BGM music=new BGM();
    JButton returnback=new JButton("返回主页");
    JButton change=new JButton("切换音乐");
    JButton close=new JButton("关闭音乐");
    Image bgimage=null;
    int count=0;
    public MusicFrame()  {
    initjButton();
    initJFrame();
    this.setVisible(true);
    }

    public void initJFrame(){
        this.setTitle("音乐设置");
        this.setFont(new Font("黑体",Font.BOLD,20));
        this.setSize(700,612);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(3);
        this.setAlwaysOnTop(true);
        try {
            bgimage= ImageIO.read(new File("Image/Musicbg.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.setLayout(null);
        JLabel jLabel=new JLabel(new ImageIcon(bgimage));
        jLabel.setBounds(0,0,this.getWidth(),this.getHeight());
        this.getContentPane().add(jLabel);
    }
    public void initjButton(){
        //位置大小
        returnback.setBounds(300, 150, 100, 50);
        change.setBounds(300,250,100,50);
        close.setBounds(300, 350, 100, 50);

        Font font=new Font("微软雅黑",Font.BOLD,16);
        Color color=new Color(153,104,97);
        returnback.setFont(font);
        change.setFont(font);
        close.setFont(font);
       returnback.setBorderPainted(true);
       returnback.setFocusPainted(false);

       change.setBorderPainted(true);
       change.setFocusPainted(false);

      close.setBorderPainted(true);
      close.setFocusPainted(false);
        //背景颜色
        returnback.setContentAreaFilled(true);
        returnback.setBackground(color);

       change.setContentAreaFilled(true);
       change.setBackground(color);

        close.setContentAreaFilled(true);
        close.setBackground(color);

        //添加监听
       returnback.addActionListener(this);
       change.addActionListener(this);
       close.addActionListener(this);

       //添加按钮
        this.getContentPane().add(returnback);
        this.getContentPane().add(change);
        this.getContentPane().add(close);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        count++;
        if(count>5)count=1;
        Object o=e.getSource();
        //System.out.println(count);
        if(o==returnback){
           this.dispose();
            new MenuView();
        }
        else if(o==change){
                music.loadMusic1(count);
        }
        else if(o==close){
            music.stopMusic();
        }
    }
}
